<?php
namespace app\Backend\controller;;

use think\Request;
use think\Db;
use think\Controller;

/**
 * Class Common
 * @package app\Backend\controller
 *
 * @property array $currentUser 当前登录用户信息
 * @property array $topMenu 顶级菜单
 * @property int $pageSize 默认每页10行,不读取config文件?
 * @property int $page 当前第1+页
 */
class Common extends Controller
{
    protected $mod,$role,$system,$nav,$menudata,$cache_model,$categorys,$module,$moduleid,$adminRules,$HrefId;

    public function _initialize()
    {

        //判断管理员是否登录
        if (!session('aid')) {
            $this->redirect('login/index');
        }
        define('MODULE_NAME',strtolower(request()->controller()));
        define('ACTION_NAME',strtolower(request()->action()));
        //权限管理
        //当前操作权限ID
        if(session('aid') !=1 ){
            $this->HrefId = db('auth_rule')->where('href',MODULE_NAME.'/'.ACTION_NAME)->value('id');
            //当前管理员权限
            $map['a.admin_id'] = session('aid');
            $this->adminRules = explode(',',session('user_rules'));
            if($this->HrefId){
                if(!in_array($this->HrefId,$this->adminRules)){
                    $this->error('您无此操作权限','index');
                }
            }
        }

        //分页参数
        $this->pageSize = 10;
        $this->page = request()->get('page',1);

        /**
         * 顶级菜单
         * @var \app\common\model\AuthRule $authRuleModel
         */
        $authRuleModel = model('auth_rule');
        $this->topMenu = $authRuleModel->getTopMenu();
        $this->assign('topMenu',$this->topMenu);

        //当前用户信息
        $this->currentUser = db('admin')->getByAdminId(session('aid'));//getByAdminId __call魔术方法
        $this->assign('currentUser',$this->currentUser);

        $this->initBackendNavigation();//左侧菜单
        $this->initSystemInfo();//系统信息
    }

    //空操作
    public function _empty(){
        $this->error('空操作，返回上次访问页面中...');
    }
    public function _error($msg=null,$code=0,$url=''){
        return array('msg'=>$msg,'code'=>$code,'url'=>$url);
    }
    public function _success($msg=null,$code=1,$url=''){
        return array('msg'=>$msg,'code'=>$code,'url'=>$url);
    }

    //获取 后台菜单列表
    private final function initBackendNavigation()
    {
        //缓存
        $authRule = cache('authRule');
        if(!$authRule){
            $authRule = db('auth_rule')->where('menu_status=1')->order('sort')->select();
            cache('authRule', $authRule, 3600);
        }

        $menus = [];
        $adminUserId = (int) session('aid');
        foreach ($authRule as $key=>$val){
            $authRule[$key]['href'] = url($val['href']);
            if($val['pid']==0){
                if( $adminUserId != 1){
                    if(in_array($val['id'],$this->adminRules)){
                        $menus[] = $val;
                    }
                }else{
                    $menus[] = $val;
                }
            }
        }
        foreach ($menus as $k=>$v){
            foreach ($authRule as $kk=>$vv){
                if($v['id']==$vv['pid']){
                    if($adminUserId != 1) {
                        if (in_array($vv['id'], $this->adminRules)) {
                            $menus[$k]['children'][] = $vv;
                        }
                    }else{
                        $menus[$k]['children'][] = $vv;
                    }
                }
            }
        }
        $this->assign('menus', \json_encode($menus,true));
    }

    //获取 系统信息
    private final function initSystemInfo()
    {
        $info = array(
            '操作系统'=> PHP_OS,
            //'运行环境'=>$_SERVER["SERVER_SOFTWARE"],
            //'PHP运行方式'=>php_sapi_name(),
            '上传附件限制' => ini_get('upload_max_filesize'),
            '执行时间限制' => ini_get('max_execution_time').'秒',
            '服务器时间'   => date('Y年n月j日 H:i:s'),
            '北京时间'     => gmdate('Y年n月j日 H:i:s',time() + 8*3600),
            '服务器域名/IP'=> $_SERVER['SERVER_NAME'].' [ '.gethostbyname($_SERVER['SERVER_NAME']).' ]',
            //'剩余空间'=>round((disk_free_space(".")/(1024*1024)),2).'M',
        );
        $this->assign('info', $info);
    }
}
